import 'package:flutter/material.dart';
import '../../../../data/api/directories_api.dart';
import '../../../../utils/formatters.dart';

/// 文件项组件，用于在文件浏览器中显示单个文件
class FileItemWidget extends StatelessWidget {
  /// 文件数据
  final FileItem file;

  /// 点击回调
  final VoidCallback onTap;

  /// 菜单操作回调
  final Function(String, FileItem) onMenuAction;

  const FileItemWidget({
    Key? key,
    required this.file,
    required this.onTap,
    required this.onMenuAction,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: _getFileTypeColor(file.fileType),
          borderRadius: BorderRadius.circular(8),
        ),
        child: _getFileIcon(),
      ),
      title: Text(file.fileName),
      subtitle: Text(
        '${Formatters.formatFileSize(file.fileSize)} • ${Formatters.formatDate(file.createTime)}',
      ),
      onTap: onTap,
      trailing: _buildMenuButton(),
    );
  }

  /// 获取文件图标
  Widget _getFileIcon() {
    switch (file.fileType) {
      case 'photo':
        return const Icon(Icons.image);
      case 'video':
        return const Icon(Icons.video_file);
      case 'audio':
        return const Icon(Icons.audio_file);
      case 'document':
        if (file.extension == '.pdf') {
          return const Icon(Icons.picture_as_pdf);
        } else if (['.doc', '.docx'].contains(file.extension)) {
          return const Icon(Icons.description);
        } else if (['.xls', '.xlsx'].contains(file.extension)) {
          return const Icon(Icons.table_chart);
        } else if (['.ppt', '.pptx'].contains(file.extension)) {
          return const Icon(Icons.slideshow);
        }
        return const Icon(Icons.insert_drive_file);
      default:
        return const Icon(Icons.insert_drive_file);
    }
  }

  /// 获取文件类型颜色
  Color _getFileTypeColor(String type) {
    switch (type) {
      case 'photo':
        return Colors.blue.withOpacity(0.2);
      case 'video':
        return Colors.red.withOpacity(0.2);
      case 'audio':
        return Colors.purple.withOpacity(0.2);
      case 'document':
        return Colors.green.withOpacity(0.2);
      default:
        return Colors.grey.withOpacity(0.2);
    }
  }

  /// 构建菜单按钮
  Widget _buildMenuButton() {
    return PopupMenuButton<String>(
      icon: const Icon(Icons.more_vert),
      onSelected: (value) => onMenuAction(value, file),
      itemBuilder: (context) {
        return [
          const PopupMenuItem<String>(
            value: 'rename',
            child: Row(
              children: [
                Icon(Icons.edit, size: 20),
                SizedBox(width: 8),
                Text('重命名'),
              ],
            ),
          ),
          const PopupMenuItem<String>(
            value: 'download',
            child: Row(
              children: [
                Icon(Icons.download, size: 20),
                SizedBox(width: 8),
                Text('下载'),
              ],
            ),
          ),
          const PopupMenuItem<String>(
            value: 'share',
            child: Row(
              children: [
                Icon(Icons.share, size: 20),
                SizedBox(width: 8),
                Text('分享'),
              ],
            ),
          ),
          const PopupMenuItem<String>(
            value: 'delete',
            child: Row(
              children: [
                Icon(Icons.delete, size: 20),
                SizedBox(width: 8),
                Text('删除'),
              ],
            ),
          ),
        ];
      },
    );
  }
}
